<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.0.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="alternate" href="/atom.xml" title="el's blog" type="application/atom+xml">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '7.5.0',
    exturl: false,
    sidebar: {"position":"right","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="切换显示服务状态、DDL、DML、备份与回复等基本操作">
<meta property="og:type" content="article">
<meta property="og:title" content="mongodb 常用操作">
<meta property="og:url" content="http:&#x2F;&#x2F;ever-lose.gitee.io&#x2F;2016&#x2F;12&#x2F;24&#x2F;database&#x2F;mongodb-command-in-common-use&#x2F;index.html">
<meta property="og:site_name" content="el&#39;s blog">
<meta property="og:description" content="切换显示服务状态、DDL、DML、备份与回复等基本操作">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-11-08T06:19:30.882Z">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://ever-lose.gitee.io/2016/12/24/database/mongodb-command-in-common-use/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>mongodb 常用操作 | el's blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">el's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">everlose's blog</h1>
      
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://ever-lose.gitee.io/2016/12/24/database/mongodb-command-in-common-use/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpeg">
      <meta itemprop="name" content="everlose">
      <meta itemprop="description" content="focus on Js、CSS、Node...etc">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="el's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          mongodb 常用操作
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2016-12-24 00:00:00" itemprop="dateCreated datePublished" datetime="2016-12-24T00:00:00+08:00">2016-12-24</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-11-08 14:19:30" itemprop="dateModified" datetime="2019-11-08T14:19:30+08:00">2019-11-08</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/database/" itemprop="url" rel="index">
                    <span itemprop="name">database</span>
                  </a>
                </span>
            </span>

          
            <div class="post-description">切换显示服务状态、DDL、DML、备份与回复等基本操作</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="基本命令"><a href="#基本命令" class="headerlink" title="基本命令"></a>基本命令</h2><h3 id="显示当前数据库服务器上的数据库"><a href="#显示当前数据库服务器上的数据库" class="headerlink" title="显示当前数据库服务器上的数据库"></a>显示当前数据库服务器上的数据库</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">show dbs</span><br></pre></td></tr></table></figure>

<h3 id="切换到指定数据库的上下文，可以在此上下文中管理testdb数据库以及其中的集合等"><a href="#切换到指定数据库的上下文，可以在此上下文中管理testdb数据库以及其中的集合等" class="headerlink" title="切换到指定数据库的上下文，可以在此上下文中管理testdb数据库以及其中的集合等"></a>切换到指定数据库的上下文，可以在此上下文中管理testdb数据库以及其中的集合等</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">use testdb</span><br></pre></td></tr></table></figure>

<h3 id="显示数据库中所有的集合（collection）"><a href="#显示数据库中所有的集合（collection）" class="headerlink" title="显示数据库中所有的集合（collection）"></a>显示数据库中所有的集合（collection）</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">show collections</span><br></pre></td></tr></table></figure>

<h3 id="查看数据库服务器的状态"><a href="#查看数据库服务器的状态" class="headerlink" title="查看数据库服务器的状态"></a>查看数据库服务器的状态</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.serverStatus()</span><br></pre></td></tr></table></figure>

<h3 id="查询指定数据库统计信息"><a href="#查询指定数据库统计信息" class="headerlink" title="查询指定数据库统计信息"></a>查询指定数据库统计信息</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">use fragment</span><br><span class="line"></span><br><span class="line">db.stats()</span><br></pre></td></tr></table></figure>

<h2 id="基本DDL和DML"><a href="#基本DDL和DML" class="headerlink" title="基本DDL和DML"></a>基本DDL和DML</h2><h3 id="创建数据库。"><a href="#创建数据库。" class="headerlink" title="创建数据库。"></a>创建数据库。</h3><p>直接通过use dbname来切换到这个数据库上下文下面，系统会自动延迟创建该数据库。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">use testdb</span><br><span class="line">show dbs (此刻可能testdb数据库并没有被创建)</span><br><span class="line">db （显示当前使用的数据库，结果位testdb）</span><br><span class="line">db.storeCollection.save(&#123;&apos;version&apos;:&apos;3.5&apos;, &apos;segment&apos;:&apos;e3ol6&apos;&#125;)  （插入一条数据）</span><br><span class="line">show dbs （此刻能看到testdb被创建）</span><br></pre></td></tr></table></figure>

<h3 id="删除数据库"><a href="#删除数据库" class="headerlink" title="删除数据库"></a>删除数据库</h3><p>直接使用db.dropDatabase()即可删除数据库。</p>
<h3 id="创建集合"><a href="#创建集合" class="headerlink" title="创建集合"></a>创建集合</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.createCollection(&apos;replicationColletion&apos;, &#123;&apos;capped&apos;:true, &apos;size&apos;:10240, &apos;max&apos;:17855200&#125;)</span><br><span class="line">show collections</span><br></pre></td></tr></table></figure>

<h3 id="删除集合。"><a href="#删除集合。" class="headerlink" title="删除集合。"></a>删除集合。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.mycoll.drop()</span><br></pre></td></tr></table></figure>

<h3 id="增加纪录。"><a href="#增加纪录。" class="headerlink" title="增加纪录。"></a>增加纪录。</h3><h4 id="两种插入方法"><a href="#两种插入方法" class="headerlink" title="两种插入方法"></a>两种插入方法</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.storeCollection.save(&#123;&apos;version&apos;:&apos;3.5&apos;, &apos;segment&apos;:&apos;e3ol6&apos;&#125;)</span><br><span class="line">db.storeCollection.insert(&#123;&apos;version&apos;:&apos;3.5&apos;, &apos;segment&apos;:&apos;e3ol6&apos;&#125;)</span><br></pre></td></tr></table></figure>

<p>若新增的数据中存在主键 ，insert() 会提示错误，而save() 则更改原来的内容为新内容。如：</p>
<p>已存在数据： <code>{_id : 1, &quot; name &quot; : &quot; n1 &quot; }</code>，再次进行插入操作时，<code>insert({_id : 1, &quot; name &quot; : &quot; n2 &quot; })</code>会报主键重复的错误提示，<code>save({ _id : 1, &quot; name &quot; : &quot; n2 &quot; })</code>     会把 n1 修改为  n2  。</p>
<p>相同点：</p>
<p>若新增的数据中没有主键时，会增加一条记录。</p>
<p>已存在数据： <code>{ _id : 1, &quot; name &quot; : &quot; n1 &quot; }</code>，再次进行插入操作时，</p>
<p><code>insert({ &quot; name &quot; : &quot; n2 &quot; })</code>    插入的数据因为没有主键，所以会增加一条数据<br><code>save({  &quot; name &quot; : &quot; n2 &quot; })</code>   增加一条数据。</p>
<h4 id="循环插入"><a href="#循环插入" class="headerlink" title="循环插入"></a>循环插入</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">for (var i = 1; i &lt; 100; i++) &#123;</span><br><span class="line">    db.testCollection.insert(&#123;</span><br><span class="line">        age : i % 7,</span><br><span class="line">        name : &apos;name&apos; + Math.round((10 + 20 * Math.random())),</span><br><span class="line">        password : &apos;123456&apos;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="更新记录。"><a href="#更新记录。" class="headerlink" title="更新记录。"></a>更新记录。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">db.testCollection.update(&#123;age: 6&#125;, &#123;$inc: &#123;age: 1&#125;&#125;) //选择age为6的一条记录，使他的age加1.</span><br><span class="line">db.testCollection.update(&#123;age: 7&#125;, &#123;$set: &#123;password : &apos;456789&apos;&#125;&#125;) //选择age为7的一条记录，设置password为456789</span><br><span class="line"></span><br><span class="line">//如果条件不匹配一个记录，希望能往数据库里新增一个，设置update函数第三个参数为true就可以。这里age为8条件匹配不到数据。</span><br><span class="line">db.testCollection.update(&#123;age: 8&#125;, &#123;$set: &#123;password : &apos;888888&apos;&#125;&#125;, true)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">//若要批量更新，设置update函数第四个参数为true就可以了。</span><br><span class="line">db.testCollection.update(&#123;age: 7&#125;, &#123;$set: &#123;password : &apos;456789&apos;&#125;&#125;, true, true)</span><br></pre></td></tr></table></figure>

<p>更新version为3.5的记录的segment值。</p>
<h3 id="查询一条纪录。"><a href="#查询一条纪录。" class="headerlink" title="查询一条纪录。"></a>查询一条纪录。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.storeCollection.findOne(&#123;&apos;version&apos;:&apos;3.5&apos;&#125;)</span><br></pre></td></tr></table></figure>
<p>参数为查询条件，可选，系统会随机查询获取到满足条件的一条记录（如果存在查询结果数量大于等于1）。</p>
<h3 id="查询多条记录。"><a href="#查询多条记录。" class="headerlink" title="查询多条记录。"></a>查询多条记录。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.storeCollection.find()</span><br></pre></td></tr></table></figure>

<p>使用find()函数，参数指定查询条件，不指定条件则查询全部记录。</p>
<p>条件查询包括4种方法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">// 1. $gt（&amp;gt;）、$gte（&amp;gt;=）、$lt（&amp;lt;）、$lt（&amp;lt;=）、$ne（!=）、没有特殊关键字（＝）</span><br><span class="line"></span><br><span class="line">db.testCollection.find(&#123;age: &#123;$$gt:5&#125;&#125;).count() //14。note：去掉gt前面一个$符号，$符号会被解析，wiznote搞的鬼！！！</span><br><span class="line"></span><br><span class="line">db.testCollection.find(&#123;age: &#123;$gte:5&#125;&#125;).count() //28</span><br><span class="line"></span><br><span class="line">db.testCollection.find(&#123;age: &#123;$lt:5&#125;&#125;).count() //71</span><br><span class="line"></span><br><span class="line">db.testCollection.find(&#123;age: &#123;$lte:5&#125;&#125;).count() //85</span><br><span class="line"></span><br><span class="line">db.testCollection.find(&#123;age: &#123;$ne:5&#125;&#125;).count() //85</span><br><span class="line"></span><br><span class="line">db.testCollection.find(&#123;age: 5&#125;).count() //14</span><br><span class="line"></span><br><span class="line">// 2. 没有特殊关键字（&amp;amp;&amp;amp;）、 $or（||）、$in（）、$nin（）</span><br><span class="line"></span><br><span class="line">db.testCollection.find(&#123;age: &#123;$$gt: 5&#125;, password:&apos;123456&apos;&#125;).count() //年龄大于5且密码为123456， 14。</span><br><span class="line"></span><br><span class="line">//年龄为5或者名字为name26，18</span><br><span class="line">db.testCollection.find(&#123;$or:[&#123;age: 5&#125;,  &#123;name:&apos;name26&apos;&#125;]&#125;).count() </span><br><span class="line"></span><br><span class="line">db.testCollection.find(&#123;age: &#123;$in: [1,5, 6]&#125;&#125;).count() //年龄在1、5、6中，43。</span><br><span class="line"></span><br><span class="line">db.testCollection.find(&#123;age: &#123;$nin: [1,5, 6]&#125;&#125;).count() //年龄不在1、5、6中，56</span><br><span class="line"></span><br><span class="line">// 3. 正则表达式匹配，威力强劲</span><br><span class="line">db.testCollection.find(&#123;name: /^name1\d/&#125;).count() //名字以name1开头的，从name10到name19的记录。41</span><br><span class="line"></span><br><span class="line">//4. where语句，大招来了</span><br><span class="line">db.testCollection.find(&#123;$where:function()&#123;return this.age &amp;gt; 5&#125; &#125;).count()</span><br></pre></td></tr></table></figure>

<h3 id="删除纪录"><a href="#删除纪录" class="headerlink" title="删除纪录"></a>删除纪录</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.storeCollection.findOne(&#123;&apos;version&apos;: &apos;3.5&apos;&#125;)   （删除version为3.5的纪录）</span><br><span class="line">db.storeCollection.findOne(&#123;&apos;version&apos;: &apos;3.5&apos;&#125;)   （返回结果为空）</span><br></pre></td></tr></table></figure>

<h3 id="统计集合记录数"><a href="#统计集合记录数" class="headerlink" title="统计集合记录数"></a>统计集合记录数</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.storeCollection.count()</span><br></pre></td></tr></table></figure>

<h3 id="查询并统计结果记录数"><a href="#查询并统计结果记录数" class="headerlink" title="查询并统计结果记录数"></a>查询并统计结果记录数</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.storeCollection.find(&#123;&apos;segment&apos;:&apos;e30l8&apos;&#125;).count()</span><br></pre></td></tr></table></figure>


<h2 id="数据库备份与恢复"><a href="#数据库备份与恢复" class="headerlink" title="数据库备份与恢复"></a>数据库备份与恢复</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 导出数据库，把线上叫的 ruler 集合数据库导出到本地的/data/db</span></span><br><span class="line">$ sudo mongodump -h 172.17.40.x -p 27017 -d ruler -o /data/db/</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数据库恢复，把 /data/db/ruler 给导入本地的 ruler 数据库</span></span><br><span class="line">$ mongorestore -h 127.0.0.1:27017 -d ruler --dir /data/db/ruler/</span><br><span class="line"></span><br><span class="line"><span class="comment"># 此外还可以了解一下向表导入导出数据的</span></span><br><span class="line"><span class="comment"># mongoimport 和 mongoexport</span></span><br></pre></td></tr></table></figure>

<h2 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h2><ul>
<li><a href="http://blog.csdn.net/templar1000/article/details/25310299" target="_blank" rel="noopener">csdn</a></li>
<li><a href="http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html" target="_blank" rel="noopener">cnblogs</a></li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
                <a href="/2016/12/24/database/mysql-command-in-common-use/" rel="next" title="mysql 常用命令">
                  <i class="fa fa-chevron-left"></i> mysql 常用命令
                </a>
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
                <a href="/2016/12/24/database/mongodb-install/" rel="prev" title="mongodb 安装与启动">
                  mongodb 安装与启动 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          
    <div class="comments" id="gitalk-container"></div>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#基本命令"><span class="nav-number">1.</span> <span class="nav-text">基本命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#显示当前数据库服务器上的数据库"><span class="nav-number">1.1.</span> <span class="nav-text">显示当前数据库服务器上的数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#切换到指定数据库的上下文，可以在此上下文中管理testdb数据库以及其中的集合等"><span class="nav-number">1.2.</span> <span class="nav-text">切换到指定数据库的上下文，可以在此上下文中管理testdb数据库以及其中的集合等</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#显示数据库中所有的集合（collection）"><span class="nav-number">1.3.</span> <span class="nav-text">显示数据库中所有的集合（collection）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看数据库服务器的状态"><span class="nav-number">1.4.</span> <span class="nav-text">查看数据库服务器的状态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查询指定数据库统计信息"><span class="nav-number">1.5.</span> <span class="nav-text">查询指定数据库统计信息</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#基本DDL和DML"><span class="nav-number">2.</span> <span class="nav-text">基本DDL和DML</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建数据库。"><span class="nav-number">2.1.</span> <span class="nav-text">创建数据库。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除数据库"><span class="nav-number">2.2.</span> <span class="nav-text">删除数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建集合"><span class="nav-number">2.3.</span> <span class="nav-text">创建集合</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除集合。"><span class="nav-number">2.4.</span> <span class="nav-text">删除集合。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#增加纪录。"><span class="nav-number">2.5.</span> <span class="nav-text">增加纪录。</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#两种插入方法"><span class="nav-number">2.5.1.</span> <span class="nav-text">两种插入方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#循环插入"><span class="nav-number">2.5.2.</span> <span class="nav-text">循环插入</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#更新记录。"><span class="nav-number">2.6.</span> <span class="nav-text">更新记录。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查询一条纪录。"><span class="nav-number">2.7.</span> <span class="nav-text">查询一条纪录。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查询多条记录。"><span class="nav-number">2.8.</span> <span class="nav-text">查询多条记录。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除纪录"><span class="nav-number">2.9.</span> <span class="nav-text">删除纪录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#统计集合记录数"><span class="nav-number">2.10.</span> <span class="nav-text">统计集合记录数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查询并统计结果记录数"><span class="nav-number">2.11.</span> <span class="nav-text">查询并统计结果记录数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据库备份与恢复"><span class="nav-number">3.</span> <span class="nav-text">数据库备份与恢复</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考链接"><span class="nav-number">4.</span> <span class="nav-text">参考链接</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <img class="site-author-image" itemprop="image" alt="everlose"
    src="/images/avatar.jpeg">
  <p class="site-author-name" itemprop="name">everlose</p>
  <div class="site-description" itemprop="description">focus on Js、CSS、Node...etc</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">97</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="feed-link motion-element">
    <a href="/atom.xml" rel="alternate">
      <i class="fa fa-rss"></i>RSS
    </a>
  </div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/everlose" title="GitHub &amp;rarr; https:&#x2F;&#x2F;github.com&#x2F;everlose" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/mailto:ever-lose@foxmail.com" title="E-Mail &amp;rarr; mailto:ever-lose@foxmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">everlose</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.0.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> v7.5.0
  </div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/muse.js"></script>
<script src="/js/next-boot.js"></script>



  




  <script src="/js/local-search.js"></script>













  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID: '48fe7d5cb2ac7e2c7fbf',
      clientSecret: '8f67b574218201d3b3ec6efe69276902e711e655',
      repo: 'everlose-blog',
      owner: 'everlose',
      admin: [''],
      id: '9fa4973e31ae95c301767188857efa68',
        language: 'zh-CN',
      distractionFreeMode: 'true'
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
</script>

</body>
</html>
